<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="bh-btn-group">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <div class="bh-search">
                            <el-select size="medium" v-model="searchCate.id" placeholder="选择页面(默认)"
                                       @change="changeSearchCate">
                                <!--<el-option label="综合统计" :value="null"></el-option>-->
                                <el-option v-for="(item, index) in catesTop" :key="index" :label="item.name"
                                           :value="item.id"></el-option>
                            </el-select>
                            <el-date-picker
                                v-model="dateValue"
                                align="right"
                                type="daterange"
                                unlink-panels
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                :picker-options="pickerOptions1" @change="pickerChange">
                            </el-date-picker>
                        </div>
                    </div>

                    <template>
                        <el-table
                            element-loading-text="努力加载中..."
                            element-loading-spinner="el-icon-loading"
                            :data="tableData"
                            style="width: 100%"
                            :border="true">
                            <el-table-column v-if="cates.length > 0" prop="date" label="日期">
                            </el-table-column>
                            <!-- 自定义列的遍历-->
                            <el-table-column v-for="(item, index) in cates" :key="index" :label="item.name">
                                <!-- 数据的遍历  scope.row就代表数据的每一个对象-->
                                <template slot-scope="scope">
                                    <div v-if="searchCate.id > 0">
                                        <div v-for="totalRow in scope.row.cids">
                                            <span v-if="item.id === totalRow.cid">{{totalRow.total}}</span>
                                        </div>
                                        <span v-if="!scope.row._cids.includes(item.id)">0</span>
                                    </div>
                                    <div v-else-if="item.total">
                                        <div v-if="item.total.length > 0">
                                            <div v-for="totalRow in item.total">
                                                <span v-if="scope.row.date === totalRow.date">{{totalRow.total}}</span>
                                            </div>
                                            <span v-if="!item._dates.includes(scope.row.date)">0</span>
                                        </div>
                                        <span v-else>0</span>
                                    </div>
                                    <span v-else>0</span>
                                </template>
                            </el-table-column>


                        </el-table>
                    </template>
                </div>
            </section>
        </el-main>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {CountLogList, CountCate} from '../../utils/axios';
    import {VueCropper} from 'vue-cropper';
    import {formatDate} from '../../utils/date';

    export default {
        components: {vBreadcrumb, VueCropper},
        data() {
            return {
                breadcumb: [{name: "数据统计"}],
                updateDialogTile: '',
                tableData: [],//列表数据
                isNull: true,
                search: {start: null, end: null},//日期筛选
                searchCate: {id: 1},//筛选
                catesTop: [],//顶级分类
                updateDialog: false,
                cates: [],
                pickerOptions1: {
                    shortcuts: [{
                        text: '所有',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(0);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '今天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '对比昨天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                dateValue: '',
            }
        },
        activated() {
            this.$store.dispatch('setActive', '/count');
            this.getCatesTop();
            this.getCates();
            // this.list();
        },
        computed: {
            ...mapGetters(['active', 'mainHeight', 'loading', 'upload']),
        },
        methods: {
            //获取顶级分类
            getCatesTop() {
                CountCate({params: {id: 0, start: this.search.start, end: this.search.end}}).then((response) => {
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.catesTop = response.data;
                    }
                })
            },
            //获取分类
            getCates() {
                CountCate({
                    params: {
                        id: this.searchCate.id,
                        start: this.search.start,
                        end: this.search.end
                    }
                }).then((response) => {
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.cates = response.data;
                        if (this.cates.length > 0) {
                            this.list();
                        } else {
                            this.tableData = null;
                        }
                    }
                })
            },
            //获取列表
            list() {
                this.$store.dispatch('setLoading', true);
                CountLogList({params: this.search}).then((response) => {
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        if (this.cates.length > 0) {
                            this.tableData = response.data;
                        } else {
                            this.tableData = null;
                        }

                        this.updateDialog = false;
                    }
                })
            },
            changeSearchCate() {
                this.getCates();
            },
            pickerChange() {
                if(this.dateValue){
                    var dateArr = this.dateValue;
                    this.search.start = formatDate(dateArr[0], 'yyyy-MM-dd');
                    this.search.end = formatDate(dateArr[1], 'yyyy-MM-dd');
                }else {
                    this.search.start = null;
                    this.search.end = null;
                }
                this.getCates();
            }
        },
        filters: {
            channel: function (val, data) {
                if (!val) return '';
                let name = '';
                data.filter((item) => {
                    if (item.id === val) {
                        name = item.name;
                    }
                });
                return name;
            }
        }
    }
</script>
